package cn.xtits.design.init;

import cn.xtits.design.config.WebResourceAppConfig;
import cn.xtits.xtf.web.springmvc.ExceptionHandlerResolver;
import cn.xtits.xtf.web.springmvc.JsonMessageConverter;
import cn.xtits.xtf.web.springmvc.RequestContextInterceptor;
import cn.xtits.xtf.web.springmvc.RequestLogInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @version 1.0 https://www.cnblogs.com/exmyth/p/10743378.html
 * @author: Bo
 * @fileName: WebAppConfig
 * @createDate: 2019-07-03 14:13.
 * @description: web配置,@EnableWebMvc或继承WebMvcConfigurationSupport类(MVC自动配置不会生效，static不能访问)
 */
//@EnableWebMvc
@Configuration
public class WebAppConfig implements WebMvcConfigurer {

    @Value("${APP_TOKEN}")
    private String APP_TOKEN;

    @Value("${spring.application.name}")
    private String applicationName;

    /**
     * 配置拦截器
     *
     * @param registry InterceptorRegistry
     * @author lance
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new RequestContextInterceptor()).addPathPatterns("/**");
        registry.addInterceptor(new RequestLogInterceptor()).addPathPatterns("/**");
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false);
    }

    @Override
    public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
        exceptionResolvers.add(new ExceptionHandlerResolver());
    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.clear();
        converters.add(new JsonMessageConverter());
    }

    /**
     * 文件上传配置
     */
    /*@Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //单个文件最大 KB,MB
        factory.setMaxFileSize(DataSize.ofMegabytes(10));
        /// 设置总上传数据总大小
        factory.setMaxRequestSize(DataSize.ofMegabytes(100));
        return factory.createMultipartConfig();
    }*/

    @Autowired
    private WebResourceAppConfig webResourceAppConfig;

    /**
     * 添加静态资源访问路径
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        WebResourceAppConfig.ResourceConfig resourceConfig = webResourceAppConfig.getResourceConfig();
        registry.addResourceHandler(resourceConfig.getPath()).addResourceLocations(resourceConfig.getResourceLocations());
    }

}
